package Ontology;

import dbcontext.Connect;
import dbcontext.ClassTable;
import dbindex.Language;
import topindex.Index;

/** Clear MySQL context database:
 * delete FROM class
 * delete FROM class_attr
 * delete FROM hierarchy 
 * delete from acon
 * 
 * # unique class 'Thing' whith parent_id=0
 * DELETE FROM term WHERE id<>36
 *   OR
 * INSERT INTO term (id,lang,text,description) VALUES (36,0,"Thing","Thing")
 * 
 * INSERT INTO term (id,lang_id,text,description) VALUES (37,0,"context_139","context_139")
 * INSERT INTO term (id,lang_id,text,description) VALUES (38,0,"context_183","context_183")
 * 
 * INSERT INTO class (parent_id,term_id,onto_id,base_onto_id) VALUES (0,36,139,133)
 * INSERT INTO class (parent_id,term_id,onto_id,base_onto_id) VALUES (0,36,183,133)
 * 
 * INSERT INTO ontology (id,term_id,type_id) VALUES (139,37,4)
 * INSERT INTO ontology (id,term_id,type_id) VALUES (183,38,4)
 * 
 * run main ()
 * 
 * # all classes (except Thing) should have parent id = 89148 or 89149 (i.e. classes which have term id 37, 38)
 * 1) onto_id = 139
 * SELECT * FROM class WHERE onto_id=139 AND term_id=36 AND parent_id=0
 * id=89152
 * UPDATE class SET parent_id=89152 WHERE onto_id=139 AND term_id<>36 AND parent_id=0
 * SELECT * FROM class WHERE onto_id=139 AND term_id<>36 AND parent_id=0
 * null
 *
 * 2) onto_id = 183
 * SELECT * FROM class WHERE onto_id=183 AND term_id=36 AND parent_id=0
 * id=89153
 * UPDATE class SET parent_id=89153 WHERE onto_id=183 AND term_id<>36 AND parent_id=0
 * SELECT * FROM class WHERE onto_id=183 AND term_id<>36 AND parent_id=0
 * null
 *
 * 1 and 2)
 * SELECT * FROM class where parent_id=0
 * 2 records
 */
public class testSlice {

    static WDSliceModeller WDSMTest;
	
    public static void printStatistics(WDSliceModeller WDSMTest) {
        System.out.println( "Slice:");
		System.out.println( "# of classes: " + WDSMTest.vResClasses.size() );
		System.out.println( "# of attributes: " + WDSMTest.vResAttr.size() );
		System.out.println( "# of pairs of classes and attributes: " + WDSMTest.vResClassAttr.size() );
		System.out.println( "# of hierarchical relations: " + WDSMTest.vResHier.size() );
		System.out.println( "# of associative relations: " + WDSMTest.vResAssoc.size() );
		System.out.println( "# of functional constraints: " + WDSMTest.vResFunc.size() );
		System.out.println( "Ontology:");
		System.out.println( "# of classes: " + WDSMTest.WDO.vClasses.size() );
		System.out.println( "# of attributes: " + WDSMTest.WDO.vAttributes.size() );
		System.out.println( "# of pairs of classes and attributes: " + WDSMTest.WDO.vClassAttr.size() );
		System.out.println( "# of hierarchical relations: " + WDSMTest.WDO.vWDHier.size() );
		System.out.println( "# of associative relations: " + WDSMTest.WDO.vWDAssoc.size() );
		System.out.println( "# of functional constraints: " + WDSMTest.WDO.vWDFunc.size() );
    }
    
    public static void main(String[] args) {
    
        int [] iSelClasses = null;
        int [] iSelAttributes = null;
        int iOntoID;
        int iBaseOntoIDTMO;
        int iLangID;
        
        //	ID of language to extract names and values of elements
        iLangID = 0;
        
        java.sql.Connection webdeso_conn = dbcontext.Connect.getDBConnection(Index.WEBDESO_HOST, Index.WEBDESO_USER, Index.WEBDESO_PASS);
        java.sql.Connection context_conn = dbcontext.Connect.getDBConnection(Connect.CONTEXT_DB, Connect.CONTEXT_USER, Connect.CONTEXT_PASS);
        Language lang = Language.get(iLangID);
        
        boolean b_test1 = true;
        if(b_test1) {
            //	IDs of extracted from request attributes
            iSelAttributes =  new int [0];
            
            //	ID of ontology
            iOntoID = 139;
            
            //	ID of TMO used to build ontology
            iBaseOntoIDTMO = 133;
                
            //	IDs of extracted from request classes
            iSelClasses =  new int [1];
            //iSelClasses[0] = 84197; 	//	63218;
            
            // problem 1: there are 12 terms "Fire" in webdeso.term: SELECT * FROM term where text="Fire"
            // problem 2: threre are 6 classes titled "Fire": SELECT * FROM class,term where class.term_id=term.id AND text="Fire"
            //ClassTable c_fire = ClassTable.get(webdeso_conn, lang, iOntoID, "Fire");
            //assert(c_fire == null);
            iSelClasses[0] = 84197; // for onto_id=139
            
        } else {
            //	IDs of extracted from request classes
            iSelClasses =  new int [2];
            iSelClasses[0] = 89130;
            iSelClasses[1] = 89144;
            
            //	IDs of extracted from request attributes
            iSelAttributes =  new int [0];
            
            //	ID of ontology
            iOntoID = 183;  // 183: Geometrical figures (Geometry AO constituent)
            
            //	ID of TMO used to build ontology
            iBaseOntoIDTMO = 133;
        }
        
        WDSMTest = new WDSliceModeller( iOntoID, iSelClasses, iSelAttributes, false, iLangID, iBaseOntoIDTMO);
        printStatistics(WDSMTest);
        WDSMTest.saveSliceToFile("./docs/slice.txt");
        
        // save to database 'context'
        WDSMTest.saveSliceToDatabase(webdeso_conn, context_conn, lang, iOntoID, iBaseOntoIDTMO);
        
        // wdfTemp.getRelationType()
        // wdfTemp.getRelationID() == iClass1ID
        // iMethodID = wdfTemp.getFuncValue();
    }
}
